<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="shortcut icon" href="../images/yc.png" type="image/x-icon">
<link rel="stylesheet" href="../css/layui.css" media="all">
</head>
<body>
	<br />
	<form class="layui-form" id="myform" style="width: 90%"
		onsubmit="return false">

		<div class="layui-form-item">
			<label class="layui-form-label">法规标题</label>
			<div class="layui-input-inline" style="width: 25%">
				<input type="text" name="title" lay-verify="required"
					lay-reqtext="标题不能为空..." placeholder="请输入标题" autocomplete="off"
					class="layui-input" style="width: 400px;" id="title">
			</div>
		</div>

		<div class="layui-form-item">
			<div class="layui-inline">
				<label class="layui-form-label">法规详情</label>
				<div class="layui-input-block">
					<textarea name="goods_form_descr" id="goods_form_descr" rows="10" cols="80"></textarea>
				</div>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button type="submit" class="layui-btn" lay-submit=""
					lay-filter="demo1">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>

<script type="text/javascript" src="../js/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="../js/layui.all.js" charset="utf-8"></script>
<script type="text/javascript" src="../js/ajaxfileupload.js" charset="utf-8"></script>
<script type="text/javascript" src="../js/showpic.js" charset="utf-8"></script>
<script type="text/javascript" src="../ckeditor/ckeditor.js"></script>
<script>
let templateList = []
CKEDITOR.replace('goods_form_descr');
layui.use(['form'], function(){
	var form = layui.form,
	layer = layui.layer;

	//获取所有模板
	$.get("/template/getAllTemplate",{},rt=>{
		if(rt.code == 200){
			let str = "";
			console.log(rt)
			if(rt.data.length > 0){
				$(".cbtselect").css("display","block");
			}
			$.each(rt.data,function(index,item){
				templateList.push(item)
				str += "<option value='"+ item.id +"'>" +item.templateName + "</option>";
			})
			$("#goodsId").append($(str));
		}
		form.render("select")
	})


	//目前确定类型为三种  即招标公告 中标公告  其他公告 就不再发送请求  直接写死
	let type = [{tno:1,tname:"招标公告"},{tno:2,tname:"中标公告"},{tno:3,tname:"其他公告"}]
	let str = ""
	type.forEach((item,index)=>{
		str += "<<option value='"+ item.tno +"'>" +item.tname + "</option>"
	})
	$("#tno").append($(str));
	form.render("select")

	
	// 自定义验证规则
	form.verify({
    	title: function(value){
      		if(value.length < 2){
        		return '帖子名称至少得2个字符...';
      		}
    	}
	});
  
	// 监听提交
  	form.on('submit(demo1)', function(data){
  		let obj = data.field;
		let descr = CKEDITOR.instances.goods_form_descr.getData();
		var myDate = new Date;
		var year = myDate.getFullYear(); //获取当前年
		var mon = myDate.getMonth() + 1; //获取当前月
		var date = myDate.getDate(); //获取当前日
		obj.createDate = year + "-" + mon + "-" + date;
		obj.createTime = year + "-" + mon + "-" + date;

		obj.tno = 4;
		obj.content = descr;
		console.log(obj)

			$.ajaxFileUpload({
				url:"/tender/add",
				secureuri:false,
				fileElementId:"fileName",
				data:obj,
				dataType:"json",
				success:function(rt,status){
					if(rt.code == 511){
						layer.msg("输入信息不完整,请确认后提交...");
						return false;
					}

					if(rt.code == 200){
						$("#myform")[0].reset();   //重置表单
						$("#showpic").html("");  //清空图片预览
						CKEDITOR.instances.goods_form_descr.setData("");
						layui.form.render();
						layer.msg("添加成功...",{icon:1,time:2000});
						return false;
					}
					layer.msg("添加帖子信息失败...",{icon:2,time:2000});
				},
				error:function(rt,status,e){
					console.log(data)
					layer.msg("添加帖子信息失败\n" + e,{icon:2,time:2000})
				}
			})
			return false;
	});

	form.on('select(goodsId)', function(e) {
		let id = $("#goodsId").val()
		console.log(templateList)
		templateList.forEach(item=>{
			if(item.id == id){
				CKEDITOR.instances.goods_form_descr.setData(item.template);
			}
		})
	})
});
</script>
</body>
</html>